package leetcode.code0718;

import leetcode.IDebug;

// https://leetcode.cn/problems/maximum-length-of-repeated-subarray/solution/java-718-zui-chang-zhong-fu-zi-shu-zu-by-beik/
// 二维dp
// dp[i][j]截至nums1中[0,i-1],nums2中[0,j-1]中以i-1,j-1结尾最长重复子数组。当前数字相等，前一个最长+1，当前数字不等就是0。
// 这其中最大的即为所求。

public class Solution implements IDebug {

	public int findLength(int[] nums1, int[] nums2) {
		int len1 = nums1.length;
		int len2 = nums2.length;
		int[][] dp = new int[len1 + 1][len2 + 1];
		int ans = Integer.MIN_VALUE;
		for (int i = 1; i <= len1; i++) {
			int n1 = nums1[i - 1];
			for (int j = 1; j <= len2; j++) {
				if (n1 == nums2[j - 1]) {
					dp[i][j] = dp[i - 1][j - 1] + 1;
				} else {
					dp[i][j] = 0;
				}
				ans = Math.max(ans, dp[i][j]);
			}
		}
		return ans;
	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
